package edu.mtu.citizenscience.mushroommapper.activity.main.fieldguide;

import java.io.IOException;
import java.util.List;

import com.androidquery.AQuery;

import edu.mtu.citizenscience.cs.utils.Convert;
import edu.mtu.citizenscience.mushroommapper.activity.R;
import edu.mtu.citizenscience.mushroommapper.activity.main.FindActivity;
import edu.mtu.citizenscience.mushroommapper.model.ModelGuideMushroom;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Field Guide Adapter adapts guide mushrooms into report list
 */
public class FieldGuideListAdapter extends ArrayAdapter<ModelGuideMushroom> {
	// android-query library
	private AQuery aq;
	// list of of guide mushrooms
	private List<ModelGuideMushroom> mushrooms;
	
	/**
	 * @since 1.0
	 * 
	 * click handler for the mushrooms expanding and collapsing
	 * the mushroom details
	 */
	private class ExpandClickListener implements OnClickListener {

		/**
		 * Called when the view is clicked
		 * 
		 * @param view - View - the view that was clicked
		 * @return void
		 */
		public void onClick(View view) 
		{
			// get the detail layout view
			View details = view.findViewById(R.id.mushroom_detail_layout);
			// get the expand indicator image view
			ImageView expandIndicator = (ImageView) view.findViewById(R.id.expand_indicator);
			// if the details layout is visible
			if (details.getVisibility() == View.VISIBLE)
			{
				// hide the details view
				details.setVisibility(View.GONE);
				// set the expand indicator as open details
				expandIndicator.setImageDrawable(aq.getContext().getResources().getDrawable(R.drawable.mushroom_details_open));
			}
			else
			{
				// show the details view
				details.setVisibility(View.VISIBLE);
				// set the expand indicator as close details
				expandIndicator.setImageDrawable(aq.getContext().getResources().getDrawable(R.drawable.mushroom_details_close));
			}
		}
		
	}
	
	/**
	 * @since 1.0
	 * 
	 * click handler for the file report button
	 * starts a new find activity
	 */
	private class FileReportHandler implements OnClickListener {
		// the mushroom that was the user wants to file the report for
		private ModelGuideMushroom mushroom;
    	
		/**
		 * Called when the view is clicked
		 * 
		 * @param mushroom - ModelGuideMushroom - the guide mushroom that was clicked
		 * @return void
		 */
    	public FileReportHandler(ModelGuideMushroom mushroom)
    	{
    		// set the mushroom for this handler
    		this.mushroom = mushroom;
    	}
    	
    	/**
		 * Called when the mushroom file report button is clicked
		 * 
		 * @param view - View - the guide mushroom that was clicked
		 * @return void
		 */
		public void onClick(View view) 
		{
			// create a intent for the Find Activity
			Intent intent = new Intent(aq.getContext(), FindActivity.class);
			// add the family name of the guide mushroom to the intent
			intent.putExtra(FindActivity.FAMILY_NAME, this.mushroom.getFamily());
			// add the genus name of the guide mushroom to the intent
			intent.putExtra(FindActivity.GENUS_NAME, this.mushroom.getGenus());
			// add the species name of the guide mushroom to the intent (might be Unknown)
			intent.putExtra(FindActivity.SPECIES_NAME, this.mushroom.getSpecies());
			// start Find activity with the intent
			aq.getContext().startActivity(intent);
		}
	}
	
	/**
	 * Create the Field Guide List Adapter
	 * 
	 * @param aq - AQuery - reference to housing activities android query instance
	 * @param mushrooms - List<ModelGuideMushroom> - list of guide mushroom models
	 * @return void
	 */
	public FieldGuideListAdapter(AQuery aq, List<ModelGuideMushroom> mushrooms) 
	{
		// create array adapter with list mushroom layout and mushroom models
		super(aq.getContext(), R.layout.field_guide_list_mushroom, mushrooms);
		
		// set the android query to instance from containing activity
		this.aq = aq;
		// set the mushroom list to past mushroom models
		this.mushrooms = mushrooms;
	}

	/**
	 * Get the view for a Field Guide List guide mushroom
	 * 
	 * @param int - position - the views position in the list
	 * @param convertView - View - the view for the list item
	 * @param parent - ViewGroup - containing list view
	 * @return View - the populated view for the list item
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		// check if the view has not been inflated before
	    if (convertView == null) 
	    {
	    	// Inflate the new view
	    	convertView = this.aq.inflate(convertView, R.layout.field_guide_list_mushroom, parent);
	    }
	    // get the model guide mushroom for this list position
	    ModelGuideMushroom mushroom = this.mushrooms.get(position);
	    
	    // Drawable to hold the guide mushroom image
	    Drawable image;
	    // check if the guide mushroom has an image
	    if (mushroom.getImage().getId() > -1)
	    {
	    	try 
	    	{
	    		// get drawable for the mushroom image
	    		Drawable thumbnail = Drawable.createFromStream(this.aq.getContext().getResources().getAssets().open(mushroom.getImage().getPath()), null);
	    		// get the image size in what ever dp equals 70px for this android device
	    		int thumbnailSize = Convert.dpToPX(this.aq.getContext(), 70);
	    		// set the thumbnail bounds from 0 to the thumbnail size
	    		thumbnail.setBounds(0, 0, thumbnailSize, thumbnailSize);
	    		// get the mushroom thumbnail image view
				ImageView mushroomThumbnail =  (ImageView) convertView.findViewById(R.id.mushroom_thumbnail);
				// set the image view thumbnail to the thumbnail drawable
				mushroomThumbnail.setImageDrawable(thumbnail);
	    		
				// get another drawable for the larger mushroom image
				image = Drawable.createFromStream(this.aq.getContext().getResources().getAssets().open(mushroom.getImage().getPath()), null);
			} 
	    	catch (IOException e) 
	    	{
	    		// if there is a problem getting the mushroom image then get the generic guide image
	    		image = this.aq.getContext().getResources().getDrawable(R.drawable.guide);
			}
	    }
	    else
	    {
	    	// if there is no mushroom image then get the generic guide image
	    	image = this.aq.getContext().getResources().getDrawable(R.drawable.guide);
	    }
	    // get the image size for an equivalent of 200 pixels on this specific device
	    int imageSize = Convert.dpToPX(this.aq.getContext(), 200);
	    // set the mushroom image size to the image size
    	image.setBounds(0, 0, imageSize, imageSize);
    	
    	// get the mushroom image image view
    	ImageView mushroomImage = (ImageView) convertView.findViewById(R.id.mushroom_image);
    	// set the image view image to the drawable image
    	mushroomImage.setImageDrawable(image);
    	
    	// get the genus species text view
    	TextView genusSpecies = (TextView) convertView.findViewById(R.id.genus_species);
    	// set the text view text to the genus + species name
    	genusSpecies.setText(mushroom.getGenus() + " " + mushroom.getSpecies());
    	
    	// get the common name text view
    	TextView commonName = (TextView) convertView.findViewById(R.id.common_name);
    	// set the text view to the mushroom name
    	commonName.setText(mushroom.getName());
    	
    	// get the mushroom description text view
    	TextView mushroomDescription = (TextView) convertView.findViewById(R.id.mushroom_description);
    	// set the text view to the mushroom description
    	mushroomDescription.setText(mushroom.getDescription());
    	
    	// get the expand indicator image view
    	ImageView expandIndicator = (ImageView) convertView.findViewById(R.id.expand_indicator);
    	// set the image view to open image
    	expandIndicator.setImageDrawable(this.aq.getContext().getResources().getDrawable(R.drawable.mushroom_details_open));
    	
    	// get the file report button 
    	Button fileReportButton = (Button) convertView.findViewById(R.id.file_report_button);
    	// attach to the button an on click listener with the file report handler and pass this mushroom model
    	fileReportButton.setOnClickListener(new FileReportHandler(mushroom));
	    
    	// get the detail layout view
    	View detailLayout = convertView.findViewById(R.id.mushroom_detail_layout);
    	// make the detail layout invisible
    	detailLayout.setVisibility(View.GONE);
    	// set the on click listener for the list item view to the expand click listener
    	convertView.setOnClickListener(new ExpandClickListener());
	    
    	// return the list item view
	    return convertView;
	}
}
